/*
 * @Author: chen haoxuan
 * @Date: 2021-06-24 21:43:59
 * @LastEditTime: 2021-06-24 21:46:37
 */
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
  void setData();
  string name;
  long num;
  int age;
  int salary;
};
void Employee::setData()
{
  cin >> num >> name >> age >> salary;
}

int main()
{
  void setData(string f);
  void addData(string f);
  void display(string f);
  void search(string f);
  setData("1.dat");
  addData("1.dat");
  display("1.dat");
  char c;
  cout << "Do you want to serch ?(y or n)";
  cin >> c;
  while (c == 'y' || c == 'Y')
  {
    search("1.dat");
    cout << endl
         << "Do you want to serch ?(y or n)";
    cin >> c;
  }
  system("pause");
  return 0;
}
void setData(string f)
{
  ofstream outfile(f, ios::out);
  if (!outfile)
  {
    cerr << "open file " << f << "failed !";
    exit(1);
  }
  Employee e[5];
  for (int i = 0; i < 5; i++)
  {
    cout << "Please enter employee " << i + 1 << ": (num,name,age,salary)";
    e[i].setData();
    outfile << e[i].num << "              " << e[i].name << "              " << e[i].age
            << "                " << e[i].salary << "\n";
  }
  cout << "set data success !" << endl;
  outfile.close();
}
void addData(string f)
{
  ofstream outfile(f, ios::out | ios::app);
  if (!outfile)
  {
    cerr << "open file " << f << "failed !";
    exit(1);
  }
  Employee e[2];
  cout << "\n";
  for (int i = 0; i < 2; i++)
  {
    cout << "Please enter add employee " << i + 1 << ": (num,name,age,salary)";
    e[i].setData();
    outfile << e[i].num << "              " << e[i].name << "              " << e[i].age
            << "                " << e[i].salary << "\n";
  }
  cout << "set add data success !" << endl;
  outfile.close();
}
void display(string f)
{
  ifstream infile(f, ios::in);
  if (!infile)
  {
    cerr << "open file " << f << "failed !";
    exit(1);
  }
  char ch;
  while (infile.get(ch))
  {
    cout << ch;
  }
  cout << "There are all data." << endl;
  infile.close();
}
void search(string f)
{
  ifstream infile(f, ios::in);
  if (!infile)
  {
    cerr << "open file " << f << "failed !";
    exit(1);
  }
  Employee e[7];
  int i;
  for (i = 0; i < 7; i++)
  {
    infile >> e[i].num >> e[i].name >> e[i].age >> e[i].salary;
  }
  long num;
  cout << "enter the num you want to search: ";
  cin >> num;
  if (num == 0)
  {
    system("pause");
    exit(0);
  }
  bool find = true;
  for (i = 0; i < 7; i++)
  {
    if (e[i].num == num)
    {
      find = false;
      cout << "It's employee " << i + 1 << ": " << e[i].num << "  " << e[i].name << "  "
           << e[i].age << "  " << e[i].salary << endl;
    }
  }
  if (find)
  {
    cout << "cann't find !" << endl;
  }
}